package dao;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import pojo.Bed;
import utils.JDBCUtils;
import utils.SystemEnum;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

public class BedDaoImpl implements BedDao{
    //床铺信息
    private Log log = LogFactory.getLog(getClass());
    @Override
    public ArrayList<Bed> findAll() {
        log.info(SystemEnum.BEGIN);
        ArrayList<Bed> list = new ArrayList<Bed>();
        Connection con = null;
        PreparedStatement statement = null;
        ResultSet rs = null;
        try {
            con = JDBCUtils.getConnection();
            String sql = "SELECT * FROM bed ";
            statement = con.prepareStatement(sql);
            rs = statement.executeQuery();
            while(rs.next()){
                Bed bed = new Bed(rs.getInt(1),rs.getInt(2),rs.getBoolean(3));
                list.add(bed);
            }
            log.info(SystemEnum.SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            log.info(SystemEnum.ERROR_INEFFECTIVE);
        }finally {
            JDBCUtils.close(con,statement,rs);
            log.info(SystemEnum.END);
        }
        return list;
    }

    @Override
    public boolean findByIdReturnBool(Integer roomId, Integer bedId) {
        log.info(SystemEnum.BEGIN);
        boolean tag = false;
        Connection con = null;
        PreparedStatement statement = null;
        ResultSet rs = null;
        try {
            con = JDBCUtils.getConnection();
            String sql = "SELECT * FROM bed WHERE dormId = ? AND bedId = ? ";
            statement = con.prepareStatement(sql);
            statement.setInt(1, roomId);
            statement.setInt(2, bedId);
            rs = statement.executeQuery();
            while(rs.next()){
                tag=true;
                break;
            }
            log.info(SystemEnum.SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            log.info(SystemEnum.ERROR_INEFFECTIVE+"\t"+SystemEnum.ERROR_MATCH);
        }finally {
            JDBCUtils.close(con,statement,rs);
            log.info(SystemEnum.END);
        }
        return tag;
    }

    @Override
    public Bed findById(Integer roomId, Integer bedId) {
        log.info(SystemEnum.BEGIN);
        Bed bed = new Bed();
        Connection con = null;
        PreparedStatement statement = null;
        ResultSet rs = null;
        try {
            con = JDBCUtils.getConnection();
            String sql = "SELECT * FROM bed WHERE dormId = ? AND bedId = ?";
            statement = con.prepareStatement(sql);
            statement.setInt(1,roomId);
            statement.setInt(2, bedId);
            rs = statement.executeQuery();
            while(rs.next()){
                bed.setDormId(rs.getInt(1));
                bed.setBedId(rs.getInt(2));
                bed.setUsed(rs.getBoolean(3));
            }
            log.info(SystemEnum.SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            log.info(SystemEnum.ERROR_INEFFECTIVE);
        }finally {
            JDBCUtils.close(con,statement,rs);
            log.info(SystemEnum.END);
        }
        return bed;
    }

    @Override
    public boolean isEmpty(Integer roomId, Integer bedId) {
        log.info(SystemEnum.BEGIN);
        Bed bed = findById(roomId, bedId);
        return bed.isUsed();
    }


    @Override
    public int insert(Connection con,Bed bed) throws SQLException {
        log.info(SystemEnum.BEGIN);
        PreparedStatement statement = null;
        int result = 0;
        try {
            String sql = "INSERT INTO bed VALUES (?,?,?) ";
            statement = con.prepareStatement(sql);
            statement.setInt(1, bed.getDormId());
            statement.setInt(2, bed.getBedId());
            statement.setBoolean(3, bed.isUsed());
            //增加一张床信息
            result = statement.executeUpdate();
            log.info(SystemEnum.SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            throw e;
        }finally {
            log.info(SystemEnum.END);
            JDBCUtils.close(null,statement,null);
        }
        return result;
    }

    @Override
    public int update(Connection con,Bed bed) throws SQLException {
        log.info(SystemEnum.BEGIN);

        PreparedStatement statement = null;

        int result = 0;
        try {
            //将床铺状态转换
            String sql = "UPDATE bed SET isUsed = !isUsed WHERE dormId = ? AND bedId = ?";
            statement = con.prepareStatement(sql);
            statement.setInt(1,bed.getDormId());
            statement.setInt(2,bed.getBedId());
            result = statement.executeUpdate();
            log.info(SystemEnum.SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            throw e;
        }finally {
            JDBCUtils.close(null,statement,null);
            log.info(SystemEnum.END);
        }
        return result;
    }

    @Override
    public int delete(Connection con,Bed bed) throws SQLException {
        log.info(SystemEnum.BEGIN);
        PreparedStatement statement = null;
        int result = 0;
        try {
            String sql = "DELETE FROM bed  WHERE dormId = ? AND bedId = ? ";
            statement = con.prepareStatement(sql);
            statement.setInt(1,bed.getDormId());
            statement.setInt(2,bed.getBedId());
            //删除床铺
            result = statement.executeUpdate();
            log.info("删除床铺\t"+SystemEnum.SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            throw e;
        }finally {
            log.info(SystemEnum.END);
            JDBCUtils.close(null,statement,null);
        }
        return result;
    }

}
